﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace MVCApplication.Helpers
{
    public class MenuHelper
    {
        public static void AddMenuOptions(Menu menu, EnumMode mode, string[][] relatedObjectsInfo, bool? prompt, System.Web.UI.Page page)
        {
			menu.Items.Clear();			
			
            //Save and return option 
            if (mode == EnumMode.Insert || mode == EnumMode.Update)
            {
                MenuItem item = new MenuItem();
                item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/saveAndClose.gif";
                item.Text = Deklarit.Resources.Resources.captionSaveAndReturnweb;
                item.NavigateUrl = string.Format("javascript:menuAction('{0}', '{1}', {2})", MenuAction.SubmitAndReturn, mode == EnumMode.Insert ? "Create" : "Edit", (prompt ?? false) ? "true" : "false");
                menu.Items.Add(item);
            }
            //return option 
            if (mode == EnumMode.Display)
            {
                MenuItem item = new MenuItem();
                item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/back.gif";
                item.Text = Deklarit.Resources.Resources.Back;
                item.NavigateUrl = "javascript:submitForm('Return', false)";
                menu.Items.Add(item);
            }
            //delete option 
            if (mode == EnumMode.Delete)
            {
                MenuItem item = new MenuItem();
                item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/delete.gif";
                item.Text = Deklarit.Resources.Resources.Delete;
                item.NavigateUrl = string.Format("javascript:menuAction('{0}', 'Delete', false)", MenuAction.Submit);
                menu.Items.Add(item);
            }
            //save option 
            if ((mode == EnumMode.Insert && !(prompt ?? false)) || mode == EnumMode.Update)
            {
                MenuItem item = new MenuItem();
                item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/save.gif";
                item.Text = Deklarit.Resources.Resources.captionSaveweb;
                item.NavigateUrl = string.Format("javascript:menuAction('{0}', '{1}', {2})", MenuAction.Submit, mode == EnumMode.Insert ? "Create" : "Edit", mode == EnumMode.Insert ? "false" : "true");
                menu.Items.Add(item);
            }
            //Save and new option 
            if (mode == EnumMode.Insert && !(prompt ?? false))
            {
                MenuItem item = new MenuItem();
                item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/saveAndNew.gif";
                item.Text = Deklarit.Resources.Resources.captionSaveAndNewweb;
                item.NavigateUrl = string.Format("javascript:menuAction('{0}', 'Create', false)", MenuAction.SubmitAndNew);
                menu.Items.Add(item);
            }
            //New option and Display option
            if (mode == EnumMode.Display || mode == EnumMode.Update)
            {
                if (relatedObjectsInfo != null && relatedObjectsInfo.Length > 0)
                {
                    MenuItem item = new MenuItem();
                    item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/new.gif";
                    item.Text = Deklarit.Resources.Resources.captionNewweb;
                    item.NavigateUrl = "javascript:void(0)";                    

                    foreach (string[] relatedObjectInfo in relatedObjectsInfo)
                    {
                        MenuItem subItem = new MenuItem();
                        subItem.Text = relatedObjectInfo[1];
                        subItem.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/" + (string.IsNullOrEmpty(relatedObjectInfo[2]) ? "bc.gif" : relatedObjectInfo[2]);
                        subItem.NavigateUrl = string.Format("javascript:submitForm('CreateRelated{0}', false)", relatedObjectInfo[0]);
                        item.ChildItems.Add(subItem);
                    }
					menu.Items.Add(item);

                    item = new MenuItem();
                    item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/display.gif";
                    item.Text = Deklarit.Resources.Resources.captionViewweb;
                    item.NavigateUrl = "javascript:void(0)";

                    foreach (string[] relatedObjectInfo in relatedObjectsInfo)
                    {
                        MenuItem subItem = new MenuItem();
                        subItem.Text = relatedObjectInfo[1];
                        subItem.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/" + (string.IsNullOrEmpty(relatedObjectInfo[2]) ? "bc.gif" : relatedObjectInfo[2]);
                        subItem.NavigateUrl = string.Format("javascript:submitForm('DisplayRelated{0}', false)", relatedObjectInfo[0]);
                        item.ChildItems.Add(subItem);
                    }
					menu.Items.Add(item);
                }
            }
            //Cancel option 
            if (mode == EnumMode.Insert || mode == EnumMode.Update || mode == EnumMode.Delete)
            {
                MenuItem item = new MenuItem();
                item.ImageUrl = GeneralFunctions.GetThemePath(page) + "Images/cancel.gif";
                item.Text = Deklarit.Resources.Resources.captionCancelweb;
                if (prompt ?? false)
                {
                    item.NavigateUrl = "javascript:window.close()";
                }
                else
                {
                    item.NavigateUrl = "javascript:submitForm('Return', false)";
                }
                menu.Items.Add(item);
            }
        }
    }
}
